<?php if ( ! defined( 'BASEPATH' ) ) exit( 'No direct script access allowed' );
/**
 * BackendPro
 *
 * A website backend system for developers for PHP 4.3.2 or newer
 *
 * @package         conlabz CMS
 * @author          Christian Schneider
 * @copyright       Copyright (c) 2009
 * @license         
 * @link            http://www.conlabz.de
 * @filesource
 */

// ---------------------------------------------------------------------------

/**
 * Events_model
 *
 * Model used to retrive webites
 *
 * @package			conlabz CMS
 * @subpackage		Models
 */
class Events_model extends Base_model {
	function Events_model( ) {
		parent::Base_model( );

		$this->_TABLES = array( 'Option' => $this->config->item( 'backendpro_table_prefix' ) . 'article' );

		// Cache to store already fetched items
		$this->_CACHE = array( );

		$this->load->model( 'categories_model' );
		$this->load->module_model( "article", "category_model" );
		$this->load->module_model( "events", "location_model" );
		// Object keyword
		// I wouldn't advise changing this, it could corrupt current
		// articles, the reason for needing this is to stop unserialze
		// errors spamming the log files
		$this->object_keyword = "BeP::Object::";

		log_message( 'debug', 'conlabz CMS : Events_model class loaded' );
	}

	/**
	 * Get Events
	 *
	 * @access public
	 * @param mixed $where Where query string/array
	 * @param array $limit Limit array including offset and limit values
	 * @return object
	 */
	function getEvents( $where = NULL, $limit = array( 'limit' => NULL, 'offset' => '' ) ) {
		$this->db->select( 'event.id, 
							event.event_type,
							event.event_location,
							event.title, 
							event.short_description,
							event.description,
							event.weekday,
							event.start_date,
							event.end_date,
							event.highlight, 
							event.active' );
		
		$this->db->from( "event" );
 		
		if( !is_null( $where ) ) {
			$this->db->where( $where );
		}
		if( !is_null( $limit['limit'] ) ) {
			$this->db->limit( $limit['limit'], ( isset( $limit['offset'] ) ? $limit['offset'] : '' ) );
		}
		$this->db->order_by( 'event.start_date asc' ); 
		return $this->db->get( );
	}
	
	function getEventView( $type_id ) {
		$return = array( );
		/*$this->db->select( 'event_menu.params' );
		$this->db->from( 'event_menu' );
		$this->db->where( 'id', $type_id );
		$event_param = $this->db->get( );
		$row = $event_param->result_array( );*/
		
		$temp_row = explode( ";", $type_id );
		$temp = array( );
		foreach( $temp_row as $key => $value ){
			$temp_value = explode( '=', $value );
			$temp[$temp_value[0]] = $temp_value[1];
		}
		$where = "`event`.`start_date` >= '".$temp['start']."'
				  AND `event`.`end_date` <= '".$temp['end']."'";
		if( !empty( $temp['highlights'] ) ){
			$where .= "`event`.`highlight` >= '1'";
		}
		$events = $this->getEvents( $where );
		foreach( $events->result_array( ) as $row ) {
			$location_query = $this->location_model->getLocations( "id = '".$row['event_location']."'" );
			$location_row = $location_query->result_array( );
			$row['event_location'] = $location_row[0]['title'];
			$return[] = $row;
		}
		
		return $return;
	}
	    
	/**
	 * Insert new Event
	 *
	 * @access public
	 * @param array $data Data array
	 */
	function insert( $data = array( ) ) {			
		$this->db->set( 'id',          		 $data['id']          		  );
		$this->db->set( 'event_type',        $data['type']                );
		$this->db->set( 'title',       		 $data['title']       		  );
		$this->db->set( 'active',      		 $data['active']      		  );
		$this->db->set( 'weekday',       	 $data['weekday']      		  );
		$this->db->set( 'event_location',    $data['location']            );
		$this->db->set( 'highlight',      	 $data['highlight']    		  );
		$this->db->set( 'description', 		 $data['description'] 		  );
		$this->db->set( 'short_description', $data['short_description']   );

		// convert start_date format
	    $tmp_start_date = explode( ".", $data['start_date'] );
		$tmp_start_date = array_reverse( $tmp_start_date );
		$data['start_date'] = implode( "-", $tmp_start_date );
	
		// convert end_date format
		$tmp_end_date = explode( ".", $data['end_date'] );
		$tmp_end_date = array_reverse( $tmp_end_date );
		$data['end_date'] = implode( "-", $tmp_end_date );
		
		// convert start_time
		$tmp_start_time = explode( ":", $data['start_time'] );
		$tmp_start_time = array_slice( $tmp_start_time, 0, count( $tmp_start_time ) + 2 );
		$data['start_time'] = implode( ":", $tmp_start_time );
		
		// convert end_time
		$tmp_end_time = explode( ":", $data['end_time'] );
		$tmp_end_time = array_slice( $tmp_end_time, 0, count( $tmp_end_time ) + 2 );
		$data['end_time'] = implode( ":", $tmp_end_time );
	
		$this->db->set( 'end_date',   $data['end_date']." ".$data['end_time'] );
		$this->db->set( 'start_date', $data['start_date']." ".$data['start_time'] );
    	
		$this->db->insert( 'event' );	
	} // insert( );	
	
	/**
	 * Update event
	 *
	 * @access public
	 * @param array $data Data array
	 */
	function update( $data = array( ), $id ) {
		$events = array( );		
		$events['title']       		 = $data['title'];
		$events['active']      		 = $data['active'];
		$events['weekday'] 			 = $data['weekday'];
		$events['highlight'] 		 = $data['highlight'];
		$events['description'] 		 = $data['description'];
		$events['short_description'] = $data['short_description'];
		
		// convert start_date format
	    $tmp_start_date = explode( ".", $data['start_date'] );
		$tmp_start_date = array_reverse( $tmp_start_date );
		$data['start_date'] = implode( "-", $tmp_start_date );
	
		// convert end_date format
		$tmp_end_date = explode( ".", $data['end_date'] );
		$tmp_end_date = array_reverse( $tmp_end_date );
		$data['end_date'] = implode( "-", $tmp_end_date );
		
		// convert start_time
		$tmp_start_time = explode( ":", $data['start_time'] );
		$tmp_start_time = array_slice( $tmp_start_time, 0, count( $tmp_start_time ) + 2 );
		$data['start_time'] = implode( ":", $tmp_start_time );
		
		// convert end_time
		$tmp_end_time = explode( ":", $data['end_time'] );
		$tmp_end_time = array_slice( $tmp_end_time, 0, count( $tmp_end_time ) + 2 );
		$data['end_time'] = implode( ":", $tmp_end_time );
		
		$events['end_date']   = $data['end_date']." ".$data['end_time'];
		$events['start_date'] = $data['start_date']." ".$data['start_time'];
	
		//insert the new event
		$this->db->update( 'event', $events, array( 'id' => $id ) );
	}
}
/* End of file events_model.php */
/* Location: ./modules/events/models/events_model.php */
